package com.tkr.config;

import com.tkr.filter.CommonFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.boot.web.servlet.ServletRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.filter.CharacterEncodingFilter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.ViewControllerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

@Configuration
public class WebMvcConfig extends WebMvcConfigurationSupport {

    //解决乱码问题
    @Bean
    public FilterRegistrationBean<CharacterEncodingFilter> getFilter(){
        CharacterEncodingFilter cfilter=new CharacterEncodingFilter();
        cfilter.setEncoding("UTF-8");
        cfilter.setForceEncoding(true);
        FilterRegistrationBean<CharacterEncodingFilter> rootFilter=new FilterRegistrationBean<>(cfilter);
        rootFilter.addUrlPatterns("/*");
        return rootFilter;
    }

    @Bean
    public FilterRegistrationBean<CommonFilter> getCommonFilter(){
        FilterRegistrationBean<CommonFilter> filterFilterRegistrationBean=new FilterRegistrationBean<>();
        filterFilterRegistrationBean.setFilter(new CommonFilter());
        filterFilterRegistrationBean.addUrlPatterns("/*");
        return  filterFilterRegistrationBean;
    }


    //配置默认的控制器
    @Override
    protected void addViewControllers(ViewControllerRegistry registry) {
        registry.addViewController("/").setViewName("redirect:/index");
    }

    /**
     * 静态资源 springmvc.xml <mvc:resouces></mvc:resouces>
     * @param registry
     * jar
     */
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations("file:G:/")
        .addResourceLocations("classpath:/static/");
    }
}
